# -*- coding: utf-8 -*-
# @Author  : Adolf  Frank
# @Time    : 2021-12-23 13:35

import os
import sys

curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0]
rootPath = os.path.split(rootPath)[0]
# 先引入目录的上级就好
sys.path.append(rootPath)

from src.db_es.ElasticSearchClass import ElasticSearchClass
from src.db_es.es_config import ES_Config

# 标准问题数据存储
def build_standard_index( index_type_):
    index_type = index_type_

    # ES 新版本使用Text替换了String类型,
    # 除特殊长文本外都使用keyword，检索尽量使用match
    index_mappings_ = {
        "mappings": {
            index_type: {
                "properties": {
                    "id": {
                        "type": "keyword",
                        "index": True
                    },
                    "user_id": {
                        "type": "text",
                        "index": True,
                    },
                    "title": {
                        "type": "text",
                        'analyzer': 'ik_max_word',
                        'search_analyzer': 'ik_max_word',
                        "index": True
                    },
                    "content_type": {
                        "type": "keyword",
                        "index": True
                    },
                    "question": {
                        "type": "text",
                        'analyzer': 'ik_max_word',
                        'search_analyzer': 'ik_max_word',
                        "index": True,
                    },
                    "answer": {
                        "type": "text",
                        'analyzer': 'ik_max_word',
                        'search_analyzer': 'ik_max_word',
                        "index": True,
                    },
                    "status": {
                        "type": "integer",
                    },
                    "create_time": {
                        # "type": "date",
                        # "format": "yyyy-mm-dd",
                        # "index": True
                        "type": "text",
                    },
                    "update_time": {
                        "type": "text",
                    },
                    # 后续为预留字段
                    "exter_1": {
                        "type": "integer",
                    },
                    "exter_2": {
                        "type": "text",
                        "index": True
                    },
                    "exter_3": {
                        "type": "text",
                        "index": True
                    },
                    "exter_4": {
                        "type": "text",
                        "index": True
                    },
                    "exter_5": {
                        "type": "text",
                        "index": True
                    },
                    "exter_6": {
                        "type": "text",
                        "index": True
                    },
                    "remarks_1": {
                        "type": "text",
                        "index": True
                    },
                    "remarks_2": {
                        "type": "text",
                        "index": True,
                    }
                }
            }
        }
    }
    return index_mappings_

# 样例数据新闻数据表
def build_news_index( index_type_):
    index_type = index_type_

    # ES 新版本使用Text替换了String类型,
    # 除特殊长文本外都使用keyword，检索尽量使用match
    index_mappings_ = {
        "mappings": {
            index_type: {
                "properties": {
                    "id": {
                        "type": "keyword",
                        "index": True
                    },
                    "user_id": {
                        "type": "text",
                        "index": True,
                    },
                    "content_ana_type": {
                        "type": "text",
                        "index": True,
                    },
                    "title": {
                        "type": "text",
                        'analyzer': 'ik_max_word',
                        'search_analyzer': 'ik_max_word',
                        "index": True,
                    },
                    "content": {
                        "type": "text",
                        'analyzer': 'ik_max_word',
                        'search_analyzer': 'ik_max_word',
                        "index": True,
                    },
                    "content_type": {
                        "type": "keyword",
                        "index": True
                    },
                    "content_link": {
                        "type": "text",
                        "index": True
                    },
                    "status": {
                        "type": "integer",
                    },
                    "create_time": {
                        # "type": "date",
                        # "format": "yyyy-mm-dd",
                        # "index": True
                        "type": "text",
                    },
                    "update_time": {
                        "type": "text",
                    },
                    # 后续为预留字段
                    "exter_1": {
                        "type": "integer",
                    },
                    "exter_2": {
                        "type": "text",
                    },
                    "exter_3": {
                        "type": "text",
                    },
                    "exter_4": {
                        "type": "text",
                    },
                    "exter_5": {
                        "type": "text",
                        "index": True
                    },
                    "exter_6": {
                        "type": "text",
                        "index": True
                    },
                    "remarks_1": {
                        "type": "text",
                    },
                    "remarks_2": {
                        "type": "text",
                        "index": True,
                    }
                }
            }
        }
    }
    return index_mappings_

def build_copper_bar(es_handle):
    es_config = ES_Config()
    index_name = es_config.index_names['logs']['index']
    index_type = es_config.type_names['type']
# 用户信息表
def build_user_info_index( index_type_):
    index_type = index_type_
    # ES 新版本使用Text替换了String类型,
    # 除特殊长文本外都使用keyword，检索尽量使用match
    index_mappings_user_info = {
        "mappings": {
            index_type: {
                "properties": {
                    "id": {
                        "type": "keyword",
                        "index": True
                    },
                    "user_name": {
                        "type": "keyword",
                        "index": True
                        # "analyzer": "ik_max_word",
                        # "search_analyzer": "ik_max_word",
                    },
                    "company_name": {
                        # 中文和英文混合时，即使用text也会 放到一起的，搜索时还使用match
                        "type": "text",
                        "index": True
                    },
                    "pass_word": {
                        "type": "text",
                        "index": True
                    },
                    "email": {
                        "type": "text",
                        "index": True
                    },
                    "avatar": {
                        "type": "text",
                        "index": True
                    },
                    "status": {
                        "type": "integer",
                    },
                    "create_time": {
                        # "type": "date",
                        # "format": "yyyy-mm-dd",
                        # "index": True
                        "type": "text",
                        "index": True
                    },
                    "update_time": {
                        "type": "text",
                        "index": True
                    },
                    # 后续为预留字段
                    # "users_info_1": {
                    #     "type": "text",
                    #     "index": True
                    # },
                }
            }
        }
    }

    return index_mappings_user_info

# 定制化数据表
def build_customer_index( index_type_):
    index_type = index_type_

    # ES 新版本使用Text替换了String类型,
    # 除特殊长文本外都使用keyword，检索尽量使用match
    index_mappings_customers = {
        "mappings": {
            index_type: {
                "properties": {
                    "id": {
                        "type": "keyword",
                        "index": True
                    },
                    "user_id": {
                        "type": "keyword",
                        "index": True
                    },
                    "title": {
                        "type": "text",
                        # "analyzer": "ik_max_word",
                        # "search_analyzer": "ik_max_word",
                        "index": True
                    },
                    "content_type": {
                        "type": "text",
                        "index": True
                    },
                    "content": {
                        "type": "text",
                        # "analyzer": "ik_max_word",
                        # "search_analyzer": "ik_max_word",
                        "index": True
                    },
                    "content_link": {
                        "type": "text",
                        "index": True
                    },
                    "content_ana_type": {
                        "type": "text",
                        "index": True
                    },
                    "status": {
                        "type": "integer",
                    },
                    "create_time": {
                        # "type": "date",
                        # "format": "yyyy-mm-dd",
                        # "index": True
                        "type": "text",
                    },
                    "update_time": {
                        "type": "text",
                    },
                    # 后续为预留字段
                    "customer_info_1": {
                        "type": "text",
                        "index": True
                    },
                    "customer_info_2": {
                        "type": "text",
                        "index": True
                    },
                    "customer_info_3": {
                        "type": "text",
                        "index": True
                    },
                    "customer_info_4": {
                        "type": "text",
                        "index": True
                    },
                    "customer_info_5": {
                        "type": "text",
                        "index": True
                    },
                    "customer_info_6": {
                        "type": "text",
                        "index": True
                    },
                    "customer_info_7": {
                        "type": "text",
                        "index": True
                    },
                    "remarks_1": {
                        "type": "integer",
                    },
                    "remarks_2": {
                        "type": "integer",
                    },
                    "remarks_3": {
                        "type": "integer",
                    }
                }
            }
        }
    }

    return index_mappings_customers

# 日志数据表
def build_logs_index( index_type_):
    index_type = index_type_

    # ES 新版本使用Text替换了String类型,
    # 除特殊长文本外都使用keyword，检索尽量使用match
    index_mappings_logs = {
        "mappings": {
            index_type: {
                "properties": {
                    "id": {
                        "type": "keyword",
                        "index": True
                    },
                    "user_id": {
                        "type": "keyword",
                        "index": True
                    },
                    "content_type": {
                        "type": "text",
                        "index": True
                    },
                    "content": {
                        "type": "text",
                        "index": True
                    },
                    "search_result": {
                        "type": "text",
                        "index": True
                    },
                    "status": {
                        "type": "integer",
                    },
                    "create_time": {
                        # "type": "date",
                        # "format": "yyyy-mm-dd",
                        # "index": True
                        "type": "text",
                    },
                    "user_reco_degree": {
                        "type": "integer",
                    },
                    "init_search_json": {
                        "type": "text",
                        "index": True
                    },

                    # 后续为预留字段
                    "logs_info_2": {
                        "type": "text",
                        "index": True
                    },
                    "logs_info_3": {
                        "type": "text",
                        "index": True
                    },
                    "logs_info_4": {
                        "type": "text",
                        "index": True
                    },
                    "logs_info_5": {
                        "type": "text",
                        "index": True
                    },
                    "logs_info_6": {
                        "type": "text",
                        "index": True
                    },
                    "logsr_info_7": {
                        "type": "text",
                        "index": True
                    },
                    "remarks_1": {
                        "type": "integer",
                    },
                    "remarks_2": {
                        "type": "integer",
                    },
                    "remarks_3": {
                        "type": "integer",
                    }
                }
            }
        }
    }

    return index_mappings_logs

if __name__ == '__main__':

################################es插入数据测试########################################
    # es_config = ES_Config()
    # es_handle_online = ElasticSearchClass(es_config.env_names['test']['HOST'], es_config.env_names['test']['PORT'], '', '')

    # # 创建 标准问题表
    # online_standard_mappings = build_standard_index(es_config.type_names['type'])
    # es_handle_online.create_index_use_mappings_body(es_config.index_names['province']['0000'], es_config.type_names['type'],
    #                                                 online_standard_mappings)

    # # 创建用户信息表
    # online_users_mappings = build_user_info_index(es_config.type_names['type'])
    # es_handle_online.create_index_use_mappings_body(es_config.index_names['user']['index'], es_config.type_names['type'],
    #                                                 online_users_mappings)

    # # 创建客户信息表
    # online_customer_mappings = build_customer_index(es_config.type_names['type'])
    # es_handle_online.create_index_use_mappings_body(es_config.index_names['customers']['index'], es_config.type_names['type'],
    #                                                 online_customer_mappings)

    # # 创建日志信息表
    # online_logs_mappings = build_logs_index(es_config.type_names['type'])
    # es_handle_online.create_index_use_mappings_body(es_config.index_names['logs']['index'], es_config.type_names['type'],
    #                                                 online_logs_mappings)

    # #删除
    # es_handle_online.delete_index(es_config.index_names['news']['index'])
    # es_handle_online.delete_index(es_config.index_names['stand']['index'])

    # # 总数目
    # print(str(es_handle_online.count(es_config.type_names['type'], es_config.index_names['stand']['index'])))
    # print("新闻数据总数", str(es_handle_online.count(es_config.type_names['type'], es_config.index_names['news']['index'])))

    # 搜索测试
    # res_list = es_handle.search(es_config.index_names['logs']['index'], 500)['hits']['hits']
    # for item in res_list:
    #    print(str(item))

    # 按着ID搜索
    # print(str(es_handle.search_used_id("kis_data_index","kis_data_index_type",142)['_source']))
    # 全量搜索，默认10条
    # print(str(es_handle_online.search(es_config.index_names['stand']['index'])))

################################ 正式环境测试 ##########################################
    es_config = ES_Config()
    es_handle_online = ElasticSearchClass(es_config.env_names['test']['HOST'], es_config.env_names['test']['PORT'],
                                          es_config.env_names['test']['USER'], es_config.env_names['test']['PASSWORD'])

    # # # 总数目
    print(str(es_handle_online.count(es_config.index_names['province']['0000'], es_config.type_names['type'])))

    # # 创建 标准问题表
    # online_standard_mappings = build_standard_index(es_config.type_names['type'])
    # es_handle_online.create_index_use_mappings_body(es_config.index_names['province']['0000'], es_config.type_names['type'],
    #                                                 online_standard_mappings)

    #######查看问答对数目#######
    # print("数据总数", str(es_handle_online.count(es_config.index_names['province']['0031'], es_config.type_names['type'])))

    # 新闻
    # online_news_mappings = build_news_index(es_config.type_names['type'])
    # es_handle_online.create_index_use_mappings_body(es_config.index_names['news']['index'], es_config.type_names['type'],
    #                     online_news_mappings)

    # 定制化
    # online_customer_mappings = build_customer_index(es_config.type_names['type'])
    # es_handle_online.create_index_use_mappings_body(es_config.index_names['customers']['index'], es_config.type_names['type'],
    #                   online_customer_mappings)

    # 用户表
    # online_user_info_mappings = build_user_info_index(es_config.type_names['type'])
    # es_handle_online.create_index_use_mappings_body(es_config.index_names['user']['index'], es_config.type_names['type'],
    #                online_user_info_mappings)

    # 日志表
    # online_logs_mappings = build_logs_index(es_config.type_names['type'])
    # es_handle_online.create_index_use_mappings_body(es_config.index_names['logs']['index'], es_config.type_names['type'],
    #                 online_logs_mappings)

    # 删除
    # es_handle_online.delete_index(es_config.index_names['province']['0000'])

# ################################# 正式环境生产 ##########################################
#     es_config = ES_Config()
#     es_handle_online = ElasticSearchClass(es_config.env_names['product']['HOST'], es_config.env_names['product']['PORT'],
#                                           es_config.env_names['product']['USER'], es_config.env_names['product']['PASSWORD'])

    # # # 总数目
    # print(str(es_handle_online.count(es_config.index_names['stand']['index'], es_config.type_names['type'])))

    # # 创建 标准问题表
    # online_standard_mappings = build_standard_index(es_config.type_names['type'])
    # es_handle_online.create_index_use_mappings_body(es_config.index_names['province']['0013'], es_config.type_names['type'],
    #                                                 online_standard_mappings)

    #######查看问答对数目#######
    # print("数据总数", str(es_handle_online.count(es_config.index_names['province']['0031'], es_config.type_names['type'])))

    # 新闻
    # online_news_mappings = build_news_index(es_config.type_names['type'])
    # es_handle_online.create_index_use_mappings_body(es_config.index_names['news']['index'], es_config.type_names['type'],
    #                     online_news_mappings)

    # 定制化
    # online_customer_mappings = build_customer_index(es_config.type_names['type'])
    # es_handle_online.create_index_use_mappings_body(es_config.index_names['customers']['index'], es_config.type_names['type'],
    #                   online_customer_mappings)

    # 用户表
    # online_user_info_mappings = build_user_info_index(es_config.type_names['type'])
    # es_handle_online.create_index_use_mappings_body(es_config.index_names['user']['index'], es_config.type_names['type'],
    #                online_user_info_mappings)

    # 日志表
    # online_logs_mappings = build_logs_index(es_config.type_names['type'])
    # es_handle_online.create_index_use_mappings_body(es_config.index_names['logs']['index'], es_config.type_names['type'],
    #                 online_logs_mappings)

    # 删除
    # es_handle_online.delete_index(es_config.index_names['province']['0031'])